<template>
<div>
  <el-button slot="reference" @click="showCompleted">转入申请已处理</el-button>
  <el-button slot="reference" @click="showUncompleted">转入申请未处理</el-button>
  <el-table :data="tableData">
    <el-table-column prop="createdTime" label="日期" width="180" :formatter="formatDate">
    </el-table-column>
    <el-table-column prop="studentName" label="姓名" width="100">
    </el-table-column>
    <el-table-column prop="outRoomBuildingName" label="转出楼栋" width="80">
    </el-table-column>
    <el-table-column prop="outRoomName" label="转出房间" width="80">
    </el-table-column>
    <el-table-column prop="inRoomBuildingName" label="转入楼栋" width="80">
    </el-table-column>
    <el-table-column prop="inRoomName" label="转入房间" width="80">
    </el-table-column>
    <el-table-column prop="leaderName" label="审核领导" width="80">
    </el-table-column>
    <el-table-column label="操作" width="120">
      <template slot-scope="scope">
        <el-button
            v-if="isCompleted"
            disabled
            size="small"
            type="success"
            >
          已完成
        </el-button>
        <el-button
            v-else
            size="small"
            type="primary"
            @click="handleApplication(scope.row)">
          分配
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="block">
    <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total=totalNum>
    </el-pagination>
  </div>
</div>
</template>

<script>
import {confirmAdjust, selectInBuildingAdjust, selectAllocationInBuildingAdjust, getOneRoom} from '../../../api/dormitory_admin'

export default {
  data () {
    return {
      pageNo: 1,
      pageSize: 5,
      DOT: {
        pageNo: 1,
        pageSize: 5,
        data: {
          ID: JSON.parse(localStorage.getItem('user')).id.toString(),
          state: ''
        }
      },
      isCompleted: true,
      totalNum: 0,
      selectForm: {
        building: '',
        inRoom: ''
      },
      tableData: []
    }
  },
  methods: {
    formatDate (row) {
      const date = new Date(row.createdTime)
      const beijingTime = new Date(date.getTime())
      const year = beijingTime.getFullYear()
      const month = (beijingTime.getMonth() + 1).toString().padStart(2, '0')
      const day = beijingTime.getDate().toString().padStart(2, '0')
      return `${year}年${month}月${day}日`
    },
    handleCurrentChange (newPage) {
      this.DOT.pageNo = newPage
      this.pageNo = newPage
      if (this.isCompleted) {
        this.getCompleted()
      } else {
        this.getUncompleted()
      }
    },
    showCompleted () {
      this.isCompleted = true
      this.DOT.pageNo = 1
      this.pageNo = 1
      this.totalNum = 0
      this.getCompleted()
    },
    getCompleted () {
      this.DOT.data.state = '已完成'
      selectInBuildingAdjust(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    showUncompleted () {
      this.isCompleted = false
      this.DOT.pageNo = 1
      this.pageNo = 1
      this.totalNum = 0
      this.getUncompleted()
    },
    getUncompleted () {
      this.DOT.data.state = '分配中'
      selectAllocationInBuildingAdjust(this.DOT).then(res => {
        this.tableData = []
        this.tableData = res.data
        console.log(this.tableData)
        this.totalNum = res.otherData.totalNum
      })
    },
    handleApplication (row) {
      let obj = {
        ID: JSON.parse(localStorage.getItem('user')).id
      }
      getOneRoom(obj).then(res => {
        if (res.data.length === 0) {
          this.$message({
            type: 'error',
            message: '该楼栋无可用房间!'
          })
        } else {
          this.$confirm(`此操作将自动分配${row.studentName}至${res.data.floor}层的${res.data.name}房间, 是否继续?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let obj = {
              ID: row.id.toString(),
              roomID: res.data.id.toString()
            }
            confirmAdjust(obj).then(res => {
              if (res.status === true) {
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
                this.showUncompleted()
              } else {
                this.$message({
                  type: 'error',
                  message: '操作失败!'
                })
              }
            })
          })
        }
      })
    }
  },
  created () {
    this.showCompleted()
  }
}
</script>
<style scoped>
</style>
